package com.example.demo.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.Entity.FriendChart;
import com.example.demo.Entity.User;
import com.example.demo.Mapper.FriendChartMapper;
import com.example.demo.Mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FriendChartServiceImp implements FriendChartService {
    @Autowired
    private FriendChartMapper friendChartMapper;

    @Autowired
    private UserMapper userMapper;
    /** 插入朋友关系 */
    @Override
    public void insertFriendChart(FriendChart friendChart){

        QueryWrapper<FriendChart> wrapper = new QueryWrapper<FriendChart>()
                .eq("self_id", friendChart.getSelfId())
                .eq("friend_id", friendChart.getFriendId());
        if(friendChart.getSelfId()==(friendChart.getFriendId())){
            throw new RuntimeException("禁止自娱自乐");
        }
        if(friendChartMapper.exists(wrapper))
        {
            throw new RuntimeException("已存在朋友关系");
        }
        FriendChart friendChart1 = new FriendChart( friendChart.getFriendId(),friendChart.getSelfId());
        friendChartMapper.insert(friendChart);
        friendChartMapper.insert(friendChart1);
    }

    /** 查找当前用户对应的所有朋友的ID */
    @Override
    public List<Integer> findAll(Integer selfId){
        QueryWrapper<FriendChart> wrapper = new QueryWrapper<FriendChart>().eq("self_id",selfId);
        return friendChartMapper.selectList(wrapper)
                .stream()
                .map(FriendChart::getFriendId)
                .collect(Collectors.toList());
    }

    /** 删除朋友关系 */
    @Override
    public void deleteFriendChart(Integer selfId, Integer friendId){
        friendChartMapper.deleteFriendChart(selfId,friendId);
        friendChartMapper.deleteFriendChart(friendId,selfId);
    }

    @Override
    public List<User> finduserTotal(Integer selfId){
        List<Integer> ints=findAll(selfId);
        System.out.println(ints);
        return findnamesbyID(ints);
    }
    @Override
    public List<User> findnamesbyID(List<Integer> ids){
        List<User> list1=new ArrayList<>();

        for(int x=0;x<ids.size();x++){

            User user1=userMapper.selectById(ids.get(x));
            list1.add(user1);
            System.out.println(list1.size());
        }
        System.out.println("最后"+list1.size());
        return list1.stream().toList();

    }
    @Override
    public void AddFriend(Integer selfId,String username){
        QueryWrapper<User> wrapper = new QueryWrapper<User>().eq("username",username);
        if (!userMapper.exists(wrapper)){
            throw new RuntimeException("用户名不存在");
        }
        Integer friendId =   userMapper.selectOne(wrapper).getId();

        FriendChart friendChart = new FriendChart(friendId,selfId);
        QueryWrapper<FriendChart> wrapper0 = new QueryWrapper<FriendChart>()
                .eq("self_id", friendChart.getSelfId())
                .eq("friend_id", friendChart.getFriendId());
        if(friendChartMapper.exists(wrapper0))
        {
            throw new RuntimeException("已存在朋友关系");
        }
        FriendChart friendChart1 = new FriendChart(selfId,friendId);
        friendChartMapper.insert(friendChart);
        friendChartMapper.insert(friendChart1);
    }
    @Override
    public boolean checkout(Integer selfId, Integer friendId){
        QueryWrapper<FriendChart> wrapper0 = new QueryWrapper<FriendChart>()
                .eq("self_id", selfId)
                .eq("friend_id", friendId);

        return  friendChartMapper.exists(wrapper0)||selfId==friendId;

    }
}
